package com.bms.modules.sys.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.bms.common.utils.R;
import com.bms.modules.sys.entity.SysOrgEntity;
import com.bms.modules.sys.service.SysOrgService;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



/**
 * 组织机构
 *
 * @author low
 * @email tolower@foxmail.com
 * @date 2019-01-13 20:20:00
 */
@RestController
@RequestMapping("sys/org")
public class SysOrgController extends AbstractController {
    @Autowired
    private SysOrgService sysOrgService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:org:list")
    public R list(@RequestParam Map<String, Object> params){
        List<SysOrgEntity> orgList = sysOrgService.selectList(
                new EntityWrapper<SysOrgEntity>()
                        .like("pids", ","+getUser().getCompanyId()+",")
                        .orderBy("sort_num", true));

        return R.ok().put("list", orgList);
    }

    /**
     * 选择机构(添加、修改菜单)
     */
    @GetMapping("/select")
    public R select(){
        //查询列表数据
        List<SysOrgEntity> orgList = sysOrgService.selectList(new EntityWrapper<SysOrgEntity>().orderBy("sort_num", true));

        //添加顶级菜单
        SysOrgEntity root = new SysOrgEntity();
        root.setId(0L);
        root.setName("组织机构");
        root.setPid(-1L);
        root.setOpen(true);
        orgList.add(root);

        return R.ok().put("orgList", orgList);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:org:info")
    public R info(@PathVariable("id") Long id){
			SysOrgEntity sysOrg = sysOrgService.selectById(id);

        return R.ok().put("sysOrg", sysOrg);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:org:save")
    public R save(@RequestBody SysOrgEntity sysOrg){
        sysOrgService.insert(sysOrg);
//
        SysOrgEntity pOrg=sysOrgService.selectById(sysOrg.getPid());
        if (sysOrg.getPid() != 0 && StringUtils.isNotBlank(pOrg.getPids())) {
            sysOrg.setPids(pOrg.getPids()+sysOrg.getId()+",");
        } else {
            sysOrg.setPids("0,"+sysOrg.getId()+",");
        }
        if (0 == sysOrg.getType()) {
            sysOrg.setCompanyId(sysOrg.getId());
        }else {
            sysOrg.setCompanyId(pOrg.getCompanyId());
        }
        sysOrgService.insertOrUpdate(sysOrg);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:org:update")
    public R update(@RequestBody SysOrgEntity sysOrg){
        SysOrgEntity pOrg=sysOrgService.selectById(sysOrg.getPid());
        if (sysOrg.getPid() != 0 && StringUtils.isNotBlank(pOrg.getPids())) {
            sysOrg.setPids(pOrg.getPids()+sysOrg.getId()+",");
        } else {
            sysOrg.setPids("0,"+sysOrg.getId()+",");
        }
        if (0 == sysOrg.getType()) {
            sysOrg.setCompanyId(sysOrg.getId());
        }else {
            sysOrg.setCompanyId(pOrg.getCompanyId());
        }
        List<SysOrgEntity> subOrgList = sysOrgService.selectList(new EntityWrapper<SysOrgEntity>().like("pids",","+sysOrg.getId().toString()+","));
        subOrgList.parallelStream().forEach(o -> {
            o.setCompanyId(sysOrg.getId());
            sysOrgService.updateById(o);
        });
        sysOrgService.updateById(sysOrg);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:org:delete")
    public R delete(@RequestBody Long[] ids){
			sysOrgService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
